package com.hc.sort.server.dao;

import java.sql.*;
import java.util.*;
import com.hc.sort.server.pojo.*;
import com.hc.sort.utils.JdbcUtil;

public class CallerDaoImpl implements CallerDao {
	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	int row = 0;

	@Override
	public int add(Caller caller) {
		return 0;
	}

	@Override
	public int delete(int id) {
		return 0;
	}

	@Override
	public int update(Caller caller) {
		return 0;
	}

	@Override
	public Caller get(int id) {
		return null;
	}

	@Override
	public List<Caller> findAll() {
		List<Caller> t=new ArrayList<Caller>();
		String sql="select * from t_caller";
		try {
			conn=JdbcUtil.getConnection();
			pstmt=conn.prepareStatement(sql);
			
			//设置参数
			rs=pstmt.executeQuery();
			
			while(rs.next()) {
				Caller caller=new Caller();
				caller.setId(rs.getInt("C_ID"));
				caller.setId(rs.getInt("C_WORKNO"));
				caller.setName(rs.getString("C_NAME"));
				caller.setSex(rs.getString("C_SEX"));
				caller.setHireDate(rs.getDate("C_HIREDATE"));
				caller.setBirthDay(rs.getDate("C_BIRTH"));
				caller.setRemark(rs.getString("C_REMARK"));
				caller.setLastLoginTime(rs.getDate("C_LAST_LOGIN_TIME"));
				caller.setLastLoginIp(rs.getString("C_LAST_LOGIN_IP"));
				
				t.add(caller);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.close(rs, pstmt, conn);
		}
		return t;
	}

	@Override
	public Caller findByWorknoAndPwd(String workNo, String password) {
		String sql="select * from t_caller where c_workno =? and c_password = ?";
		
		try {
			conn=JdbcUtil.getConnection();
			pstmt=conn.prepareStatement(sql);
			
			//传参
			pstmt.setObject(1, workNo);
			pstmt.setObject(2, password);
			
			rs=pstmt.executeQuery();
			if(rs.next()) {
				Caller caller=new Caller();
				caller.setId(rs.getInt(1));
				caller.setWorkNo(rs.getString(2));
				caller.setPassword(rs.getString(3));
				caller.setName(rs.getString(4));
				caller.setSex(rs.getString(5));
				caller.setHireDate(rs.getDate(6));
				caller.setBirthDay(rs.getDate(7));
				caller.setRemark(rs.getString(8));
				caller.setLastLoginTime(rs.getDate(9));
				caller.setLastLoginIp(rs.getString(10));
				
				return caller;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.close(rs,pstmt,conn);
		}
		return null;
	}
	
	public static void main(String[] args) {
		CallerDaoImpl dao=new CallerDaoImpl();
//		Caller caller=dao.findByWorknoAndPwd("001", "1");
//		
//		System.out.println(caller);
		
		List<Caller> callerList=dao.findAll();
		System.out.println(callerList);
	}
}
